package com.weipai.springboot.util;

import com.alibaba.fastjson.JSONObject;
import com.weipai.springboot.module.service.impl.OrderServiceImpl;
import com.weipai.springboot.util.HttpClientUtils;
import com.weipai.springboot.util.PropUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;

/**
 * @author songmengqiang
 * @create 2018-09-14 15:18
 **/

public class PushUtil {
    private static final Logger log = LoggerFactory.getLogger(PushUtil.class);
    private static String appKey;

    private static String appMasterKey;

    private static String appKeyIOS;

    private static String appMasterKeyIOS;

    private static HttpClient client = HttpClientUtils.getClient();

    private static String url = PropUtils.getString("umeng.url");

    private static String aliasType = "gonglaoda";

    static {
        appKey = PropUtils.getString("umeng.appKey");
        appMasterKey = PropUtils.getString("umeng.appMasterKey");
        appKeyIOS = PropUtils.getString("umeng.appKeyIOS");
        appMasterKeyIOS = PropUtils.getString("umeng.appMasterKeyIOS");
    }

    public static void sendAndroidAndIOS(String alias,String type, String title, String content) {
        try {
            sendAndroidCustomizedcast(alias,type, aliasType, title, content);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            sendIOSCustomizedcast(alias,type ,aliasType, title, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //notification message

    public static void main111(String[] args) {
        sendAndroidAndIOS("18532045821","customizedcast", "test11", "content");
        System.out.println("success");
    }

    public static void sendAndroidCustomizedcast(String alias,String type, String aliasType, String title,
                                                 String content) throws Exception {
        JSONObject paramMap = new JSONObject();
        paramMap.put("appkey", appKey);
        String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
        paramMap.put("timestamp", timestamp);
        if(StringUtils.isEmpty(type)){
            paramMap.put("type", "customizedcast");
        }else {
            paramMap.put("type", type);
        }
       /* paramMap.put("type", "customizedcast");*/
        paramMap.put("alias", alias);
        paramMap.put("alias_type", aliasType);

        JSONObject payload = new JSONObject();
        payload.put("display_type", "notification");

        JSONObject body = new JSONObject();
        //body.put("ticker", UserControllerUtil.getUserColumn(alias, "deviceTokens"));
        body.put("title", title);
        body.put("text", content);
        body.put("after_open", "go_custom");
        payload.put("body", body);
        paramMap.put("payload", payload);
        paramMap.put("production_mode", "true");
        send(paramMap.toJSONString(), appMasterKey);
    }

    public static void sendIOSCustomizedcast(String alias,String type ,String aliasType, String title,
                                             String content) throws Exception {
        JSONObject paramMap = new JSONObject();
        paramMap.put("appkey", appKeyIOS);
        String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
        paramMap.put("timestamp", timestamp);
        if(StringUtils.isEmpty(type)){
            paramMap.put("type", "customizedcast");
        }else {
            paramMap.put("type", type);
        }

        paramMap.put("alias", alias);
        paramMap.put("alias_type", aliasType);

        JSONObject payload = new JSONObject();
        payload.put("display_type", "notification");

        JSONObject aps = new JSONObject();
        JSONObject alert = new JSONObject();
        alert.put("title", title);
//        alert.put("subtitle","subtitle");
        alert.put("body", content);

        aps.put("alert", alert);
        aps.put("sound", "default");

        payload.put("aps", aps);

        //paramMap.put("description", "消息111");
        paramMap.put("payload", payload);
        paramMap.put("production_mode", "true");
//        paramMap.put("description","测试alias通知-iOS");
        send(paramMap.toJSONString(), appMasterKeyIOS);
    }

    public static boolean send(String body, String appMasterKey) throws Exception {

        String sign = DigestUtils.md5Hex(("POST" + url + body + appMasterKey).getBytes("utf8"));
        String postUrl = url + "?sign=" + sign;

        HttpPost post = new HttpPost(postUrl);
        post.setHeader("User-Agent", "Mozilla/5.0");
        StringEntity se = new StringEntity(body, "UTF-8");
        post.setEntity(se);
        // Send the post request and get the response
        HttpResponse response = client.execute(post);
        HttpEntity httpEntity = response.getEntity();
        String respStr = EntityUtils.toString(httpEntity, "utf-8");
        JSONObject resp = JSONObject.parseObject(respStr);
        String retCode = resp.getString("ret");
        log.info("推送结果："+retCode);

        if ("SUCCESS".equals(retCode)) {
            return true;
        }

        return false;
    }
}
